# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=libjpeg-turbo
pkgver=2.1.3
pkgrel=1
pkgdesc="Accelerated baseline JPEG compression and decompression library"
url="https://libjpeg-turbo.org/"
arch="all"
license="BSD-3-Clause IJG Zlib"
makedepends="cmake nasm samurai"
replaces="libjpeg"
subpackages="$pkgname-static $pkgname-doc $pkgname-dev $pkgname-utils"
source="https://downloads.sourceforge.net/libjpeg-turbo/libjpeg-turbo-$pkgver.tar.gz"

# secfixes:
#   2.1.0-r0:
#     - CVE-2021-20205
#   2.0.4-r2:
#     - CVE-2020-13790
#   2.0.4-r0:
#     - CVE-2019-2201
#   2.0.2-r0:
#     - CVE-2018-20330
#     - CVE-2018-19664
#   1.5.3-r3:
#     - CVE-2018-11813
#   1.5.3-r2:
#     - CVE-2018-1152


build() {
	local _floattest=
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	case "$CARCH" in
		ppc64le) export JSIMD_FORCENONE=1;;
		s390x|riscv64) _floattest="-DFLOATTEST=fp-contract";;
	esac
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DWITH_JPEG8=1 \
		$_floattest \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --build build --target install
	install -d "$pkgdir"/usr/share/licenses/$pkgname
	ln -s ../../doc/libjpeg-turbo/LICENSE.md "$pkgdir/usr/share/licenses/$pkgname"
}

utils() {
	pkgdesc="Utilities for manipulating JPEG images"
	replaces="jpeg"
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/bin "$subpkgdir"/usr/
}

doc() {
	default_doc
	replaces="jpeg-doc"
}

dev() {
	default_dev
	replaces="jpeg-dev"
}

sha512sums="
975c9835de7d70c6c8c5a1959adfa18f7f956bc9946fde8f1b40c1d853245c38a3118ac06b93841ec3e90be6945c38d687d062b371496c2ee9b5638d7f60418d  libjpeg-turbo-2.1.3.tar.gz
"
